/*
* Copyright (C) 2015 Pedro Paulo de Amorim
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.ppamorim.cult;

import com.github.ppamorim.cult.customview.CultShadowLayout;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;

import java.io.IOException;

/**
 * This is the main class of the library
 * here is inflated some views and added
 * some params to it. After is added a
 * isntance os CultHelperCallback to
 * provide a drag possibility to CultView
 * (not working yet)
 *
 * @author Pedro Paulo Amorim
 *
 */
public class CultView extends CultShadowLayout {

  private Text toolbar;

  public CultView(Context context) {
    this(context, null);
  }

  public CultView(Context context, AttrSet attrs) {
    this(context, attrs, "");
  }

  public CultView(Context context, AttrSet attrs, String defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initializeToolbar(context);
    getStyle(context, attrs, defStyleAttr);
  }

  /**
   * Override method to map some views and to configure the
   * view height, animation and to initialize DragViewHelper.
   */
//  @Override protected void onFinishInflate() {
//    super.onFinishInflate();
//    if (!isInEditMode()) {
//
//    }
//  }
// onFinishInflate  onSizeChanged onSaveInstanceState onRestoreInstanceState
  /**
   *  When the system verify that is needed to
   *  measure the view, was added a method to
   *  set the vertical drag range based on
   *  the height of the view
   */
//  @Override protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
//    super.onSizeChanged(width, height, oldWidth, oldHeight);
//  }

  /**
   * Perform the save of the instance state of some params that's used at dragView.
   * @return Parcelable
   */
//  @Override public Parcelable onSaveInstanceState() {
//    Parcelable superState = super.onSaveInstanceState();
//    SavedState ss = new SavedState(superState);
//    ss.toolbarTitle = this.toolbar.getTitle().toString();
//    return ss;
//  }

  /**
   * Called when the view is restored
   * @param state Return the state
   */
//  @Override public void onRestoreInstanceState(Parcelable state) {
//    SavedState ss = (SavedState) state;
//    super.onRestoreInstanceState(ss.getSuperState());
//    this.toolbar.setTitle(ss.toolbarTitle);
//  }


  /**
   * Initialize some attributes to provide
   * the height, padding and content to CultView
   */
  private void getStyle(Context context, AttrSet attrs, String defStyleAttr) {
    if (attrs != null) {
      //TypedArray a = context.obtainStyledAttributes(attrs, ResourceTable.styleable.cult_view, defStyleAttr, 0);
     // TypedAttribute a = context.
     // toolbar.setTitle(a.getString(R.styleable.cult_view_cult_title));
     // a.recycle();
      toolbar.setText(defStyleAttr);
    }
  }

  private void initializeToolbar(Context context) {
    if (toolbar == null) {
      toolbar = new Text(context);
      DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,
              DirectionalLayout.LayoutConfig.MATCH_PARENT);
      addComponent(toolbar,layoutConfig);
     // ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
      //    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

     // addComponent(toolbar, params);
    }
  }

  private CultView setCultColor(Element color) {
    toolbar.setBackground(color);
    return this;
  }

  private CultView setCultTitle(int titleId) {
    try {
      this.setCultTitle(getContext().getResourceManager().getResource(titleId).toString());
    } catch (IOException e) {
      e.printStackTrace();
    } catch (NotExistException e) {
      e.printStackTrace();
    }
    return this;
  }

  private CultView setCultTitle(String title) {
    toolbar.setText(title);
    return this;
  }
//TODO hos未提供保存view状态的BaseSavedState API接口
//  private static class SavedState extends BaseSavedState {
//
//    private String toolbarTitle;
//
//    SavedState(Pa Parcelable superState) {
//      super(superState);
//    }
//
//    private SavedState(Parcel in) {
//      super(in);
//      this.toolbarTitle = in.readString();
//    }
//
//    @Override public void writeToParcel(Parcel out, int flags) {
//      super.writeToParcel(out, flags);
//      out.writeString(toolbarTitle);
//    }
//
//    public static final Parcelable.Creator<SavedState> CREATOR
//        = new Parcelable.Creator<SavedState>() {
//      public SavedState createFromParcel(Parcel in) {
//        return new SavedState(in);
//      }
//
//      public SavedState[] newArray(int size) {
//        return new SavedState[size];
//      }
//    };
//  }
  @Override
  protected void init(Context context, AttrSet attrSet) {
    super.init(context,attrSet);
  }

  @Override
  public void setRoundRadius(int roundRadius) {
    super.setRoundRadius(roundRadius);
  }

  @Override
  public void setRadius(int radius) {
    super.setRadius(radius);
  }

  @Override
  public void setRippleColor(Color rippleColor){
    super.setRippleColor(rippleColor);
  }

  @Override
  public int getShapeType() {
    return mShapeType;
  }
  @Override
  public void setColorRes(int colorRes) {
    super.setColorRes(colorRes);
  }

  /**
   * Set the shape type.
   *
   * @param shapeType SHAPE_TYPE_ROUND or SHAPE_TYPE_RECTANGLE
   */
  @Override
  public void setShapeType(int shapeType) {
    mShapeType = shapeType;
    invalidate();
  }

  @Override
  public int getRadius() {
    return mRadius;
  }

  /**
   * 设置阴影大小
   *
   * @param value 阴影值
   */
  @Override
  public void setElevation(float value) {
    if (value > 5f) {
      mShadowElevation = 2;
    } else {
      mShadowElevation = 1;
    }
  }

  /**
   * 是否有阴影
   *
   * @return true 有 false 没有
   */
  @Override
  public boolean hasElevation() {
    return mShadowElevation != 0 ? true : false;
  }

}
